<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-card>
          <el-form :inline="true" :model="searchForm">
            <el-form-item label="图书名称">
              <el-input v-model="searchForm.name" placeholder="请输入内容"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="doSearch()">搜索</el-button>
              <el-button type="success" @click="handleAdd()">新增</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
    <!-- 表格和分页部分 -->
    <el-row class="mt-10">
      <el-col :span="24">
        <el-card>
          <el-table :data="pageData.list" border @selection-change="handleSelectionChange">
            <el-table-column prop="id" label="编号" width="60"></el-table-column>
            <el-table-column prop="name" label="图书名称" width="120" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="author" label="图书作者" width="120" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="time" label="创建时间"></el-table-column>
            <el-table-column label="图书类型">
              <template slot-scope="scope">
                <span v-if="scope.row.type === 1">计算机/软件</span>
                <span v-if="scope.row.type === 2">小说/文摘</span>
                <span v-if="scope.row.type === 3">杂项</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="300">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="success"
                  @click="handleEdit(scope.$index, scope.row)"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            class="mt-10"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageData.pageNo"
            :page-sizes="[2, 5, 10, 15, 20]"
            :page-size="pageData.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageData.totalCount"
          ></el-pagination>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog
      :title="(saveForm.id == ''? '新增' : '修改') + '图书信息'"
      :visible.sync="saveDialogVisible"
    >
      <!-- 表单数据绑定到 saveForm 对象 -->
      <el-form :model="saveForm" :rules="rules" ref="saveForm">
        <!-- 分类 ID 不显示，修改才需要分类 ID -->
        <el-form-item label="图书ID" label-width="120px" v-show="false">
          <!-- ID 设置为隐藏域 -->
          <el-input type="hidden" v-model="saveForm.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图书名称" label-width="120px" prop="name">
          <el-input v-model="saveForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图书作者" label-width="120px" prop="author">
          <el-input v-model="saveForm.author" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图书创建时间" label-width="120px" prop="time">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="saveForm.time"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="图书类型" label-width="120px" prop="type">
          <el-select v-model="saveForm.type" placeholder="请选择">
            <el-option label="计算机/软件" :value="1"></el-option>
            <el-option label="小说/文摘" :value="2"></el-option>
            <el-option label="杂项" :value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="saveDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import qs from "qs";
export default {
  data() {
    return {
      searchForm: {
        name: "",
        pageSize: 5,
        pageNo: 1
      },
      pageData: {
        list: [],
        //当前页码
        pageNo: 1,
        //当页显示数据
        pageSize: 2,
        totalCount: 0,
        totalPageCount: 1
      },
      //保存新增对话框的显示状态
      saveDialogVisible: false,
      saveForm: {
        id: "",
        name: "",
        author: "",
        time: "",
        type: ""
      },
      rules: {
        name: [
          { required: true, message: '图书名称不能为空', trigger: 'blur' }
        ],
        author: [
          { required: true, message: '图书作者不能为空', trigger: 'blur' }
        ],
        time: [
          { required: true, message: '图书创建时间不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '图书类型不能为空', trigger: 'change' }
        ]
      }
    };
  },
  mounted() {
    this.doSearch();
  },
  methods: {
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .get("/bookManage/deleteBiz?id=" + row.id)
            .then(response => {
              this.$message.success("删除成功");
              this.doSearch();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleEdit(index, row) {
      console.log("获取的row.id:", row.id);
      this.saveDialogVisible = true;
      this.$axios.get("/bookManage/viewBook?id=" + row.id).then(response => {
        console.log("赋值前this.saveForm:", this.saveForm);
        console.log("后端返回的数据:", response.data);
        this.saveForm = { ...response.data };
        console.log("赋值前this.saveForm:", this.saveForm);
        this.saveDialogVisible = true;
      });
    },
    doSave(saveForm) {
      console.log("saveForm中的id:", saveForm.id);
      let queryStr =qs.parse(qs.stringify(this.saveForm));
      console.log("发送给后端的修改数据:", queryStr);
      if (this.saveForm.id == "" || this.saveForm.id == null) {
        this.$axios.post("/bookManage/addBiz", queryStr).then(response => {
          this.saveDialogVisible = false;
          this.$message.success("添加成功");
          this.doSearch();
        });
      } else {
        this.$axios.post("/bookManage/updateBook", queryStr).then(response => {
          this.saveDialogVisible = false;
          console.log("xg:" + queryStr);
          this.$message.success("修改成功");
          this.doSearch();
        });
      }
    },
    handleAdd() {
      this.saveForm = {
        id: "",
        name: "",
        author: "",
        time: "",
        type: ""
      };
      this.saveDialogVisible = true;
    },
    doSearch() {
      let bodyData = qs.stringify(this.searchForm);
      this.$axios.post("/bookManage/page?" + bodyData).then(response => {
        this.pageData = response.data;
      });
    },

    handleSelectionChange() {
      console.log(val);
    },
    handleSizeChange(pageSize) {
      this.searchForm.pageSize = pageSize;
      this.doSearch();
    },
    handleCurrentChange(pageNo) {
      this.searchForm.pageNo = pageNo;
      this.doSearch();
    },
    submitForm() {
      this.$refs['saveForm'].validate((valid) => {
        if (valid) {
          this.doSave(this.saveForm);
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style>
/* 增加一个 10px 的 top 外边距样式 */
.mt-10 {
  margin-top: 10px;
}
.my-msg {
  margin-top: -30px;
  margin-right: 20px;
}
</style>
